# -*- coding: utf-8 -*-

"""Unimail
@version: 0.14
@license: GPL 3.0 (see "license" file)
@author: Jose A. Jimenez
@contact: jajimc@gmail.com"""

import sys
import os
import gtk
import appindicator
import dialabout
import dialsettings
import dialassistant
import core

DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))

class WinMain:
    """Represents the main UI window."""
    def __init__(self):
        self.__gladefile = os.path.join(DIR, "winmain.glade")
        self.__builder = gtk.Builder()
        self.__builder.add_from_file(self.__gladefile)
        self.__window = self.__builder.get_object("winMain")
        
        self.__mitemOpen = self.__builder.get_object("mitemOpen")
        self.__mitemExit = self.__builder.get_object("mitemExit")
        self.__menuTray = self.__builder.get_object("menuTray")
        self.__labEmailAddress = self.__builder.get_object("labEmailAddress")
        
        self.__builder.connect_signals(self)
        
        # Load user settings
        self.__settings = core.configuration.load_settings()
        self.__update()
        
    def show(self):
        self.__window.show_all()
        if not self.__settings.check():
            self.__show_assistant()
    
    def winmain_delete(self, widget, event, data=None):
        self.__hide_window()
        return True
        
    def winmain_destroy(self, widget):
        gtk.main_quit()
        
    def __show_window(self):
        if not self.__window.get_property("visible"):
            self.__mitemOpen.set_sensitive(False)
            self.__window.show()
            
    def __hide_window(self):
        self.__mitemOpen.set_sensitive(True)
        self.__window.hide()
            
    def mitemclose_activate(self, widget):
        self.__hide_window()
        
    def mitememailaccountsettings_activate(self, widget):
        dialSettings = dialsettings.DialSettings(self.__settings)
        result = dialSettings.run()
        
        if result == gtk.RESPONSE_OK:
            newSettings = dialSettings.get_settings()
            self.__set_settings(newSettings)
            
    def mitemassistant_activate(self, widget):
        self.__show_assistant()
            
    def __set_settings(self, settings):
        core.configuration.save_settings(settings)
        self.__settings = settings
        self.__update()
        
    def mitemabout_activate(self, widget):
        dialAbout = dialabout.DialAbout()
        dialAbout.run()
        
    def stiapp_popup_menu(self, widget, button, time):
        self.__menuTray.show_all()
        self.__menuTray.popup(None, None, gtk.status_icon_position_menu, button, time, widget)
        
    def mitemopen_activate(self, widget):
        self.__show_window()
        
    def mitemexit_activate(self, widget):
        sys.exit()
        
    def __update(self):
        if self.__settings.check():
            name = self.__settings.get_email_name()
            address = self.__settings.get_email_address()
            self.__labEmailAddress.set_text("%s <%s>" % (name, address))
        else:
            self.__labEmailAddress.set_text("Incomplete settings")
            
    def __show_assistant(self):
        dialAssistant = dialassistant.DialAssistant()
        result = dialAssistant.run()
        
        if result == gtk.RESPONSE_OK:
            newSettings = dialAssistant.get_settings()
            self.__set_settings(newSettings)
